package com.cd.miniprogramServer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cd.miniprogramServer.common.errorcode.BaseErrorCode;
import com.cd.miniprogramServer.common.exception.ServiceException;
import com.cd.miniprogramServer.dao.entity.UserDO;
import com.cd.miniprogramServer.dao.mapper.UserMapper;
import com.cd.miniprogramServer.dto.req.CreateUserReqDTO;
import com.cd.miniprogramServer.dto.req.UpdateUserReqDTO;
import com.cd.miniprogramServer.dto.resp.UserRespDTO;
import com.cd.miniprogramServer.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements UserService {
    
    private final UserMapper userMapper;
    
    @Override
    public UserDO getOneUser() {
        return userMapper.selectOneRecord();
    }
    
    @Override
    public UserRespDTO getOneUserDTO() {
        UserDO user = getOneUser();
        return BeanUtil.toBean(user, UserRespDTO.class);
    }
    
    @Override
    public Boolean createUser(CreateUserReqDTO reqDTO) {
        try {
            UserDO userDO = BeanUtil.toBean(reqDTO, UserDO.class);
            return this.save(userDO);
        } catch (Exception e) {
            throw new ServiceException("创建用户失败", BaseErrorCode.SERVICE_ERROR);
        }
    }
    
    @Override
    public Boolean updateUser(UpdateUserReqDTO reqDTO) {
        try {
            LambdaUpdateWrapper<UserDO> updateWrapper = new LambdaUpdateWrapper<UserDO>()
                    .eq(UserDO::getId, reqDTO.getId())
                    .set(UserDO::getEmail, reqDTO.getEmail())
                    .set(UserDO::getPhone, reqDTO.getPhone())
                    .set(UserDO::getNickname, reqDTO.getNickname())
                    .set(UserDO::getAvatar, reqDTO.getAvatar())
                    .set(UserDO::getGender, reqDTO.getGender());
            
            return this.update(updateWrapper);
        } catch (Exception e) {
            throw new ServiceException("更新用户失败", BaseErrorCode.SERVICE_ERROR);
        }
    }
    
    @Override
    public UserRespDTO getUserById(Long id) {
        try {
            UserDO userDO = this.getById(id);
            if (userDO == null) {
                throw new ServiceException("用户不存在", BaseErrorCode.CLIENT_ERROR);
            }
            return BeanUtil.toBean(userDO, UserRespDTO.class);
        } catch (ServiceException e) {
            throw e; // 重新抛出自定义异常
        } catch (Exception e) {
            throw new ServiceException("查询用户失败", BaseErrorCode.SERVICE_ERROR);
        }
    }
    
    @Override
    public Boolean deleteUserById(Long id) {
        try {
            return this.removeById(id);
        } catch (Exception e) {
            throw new ServiceException("删除用户失败", BaseErrorCode.SERVICE_ERROR);
        }
    }
}